package co.fitstart.scheduler.job;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import co.fitstart.scheduler.service.OrderService;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Component
public class OrderJob {
    private static final Logger logger = LoggerFactory.getLogger(OrderJob.class);
    
    @Autowired
    private OrderService orderService;
    
    /**
     * 超过48小时未付款的订单自动关闭
     */
    @Scheduled(cron = "0 0 0 * * *")
    public void scanOnlineOrderClose() {
        
        Date now = new Date();
        
        logger.info("start running OrderJob#scanOnlineOrderClose at {}", now);
        
        // scan online order close
        int toCloseOnlineOrder = orderService.toCloseOnlineOrder(now);
        logger.info("scaned {} online order need to be colsed before close online order", toCloseOnlineOrder);
        
        if(toCloseOnlineOrder > 0) {
            orderService.closeOnlineOrder(now);
        }
        
        logger.info("finish running OrderJob#scanOnlineOrderClose at {}", new Date());
        
    }
}
